package cn.windfly;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import static java.awt.SystemTray.getSystemTray;
import static java.awt.SystemTray.isSupported;

public class MainForm {
    JFrame frame;
    private JPanel mainPanel;
    private JTextField inputTime;
    private JTextArea areaContent;
    private JButton butStart;
    private JButton butStop;
    private JButton butBackRun;
    private JLabel labNowtime;
    private JLabel alertTimeLabel;
    private JLabel leftTimeLabel;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    private int waitTime;
    private long beginTime;
    private boolean doit = false;
    private void initTimer() {
        new Thread(new Runnable() {

            public void run() {
                while (true) {
                    EventQueue.invokeLater(new Runnable() {

                        public void run() {
                            labNowtime.setText(format.format(new Date()));
                            if (doit) {
                                long time = (waitTime * 1000*60 - (new Date().getTime() - beginTime));
                                alertTimeLabel.setText(format.format(beginTime + waitTime*60 * 1000));
                                leftTimeLabel.setText(time / 1000 + "");
                                if (time < 0) {
                                    doit = false;
                                    butStart.setEnabled(true);
                                    butStop.setEnabled(false);
                                    inputTime.setEditable(true);
                                    areaContent.setEditable(true);
                                    JOptionPane.showMessageDialog(null, areaContent.getText());
                                }
                            }
                        }
                    });

                    try {
                        Thread.sleep(1000);
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(null, ex, "Error!", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        }).start();
    }

    public MainForm() {
        butStart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

//                labNowtime.setText(new Date().toString());
                beginTime = new Date().getTime();
                try {
                    waitTime = Integer.valueOf(inputTime.getText());
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(mainPanel, "时间输入有错!", "T_T", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                butStart.setEnabled(false);
                butStop.setEnabled(true);
                doit = true;
            }
        });
        butStop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doit = false;
                butStart.setEnabled(true);
                butStop.setEnabled(false);
            }
        });
        butBackRun.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (isSupported()) {
                    frame.setVisible(false);
                } else {
                    JOptionPane.showMessageDialog(null, "Not support!");
                }
            }
        });
    }

    public static void main(String[] args) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        final JFrame frame = new JFrame("MainForm");
        MainForm mainForm = new MainForm();
        mainForm.frame = frame;
        frame.setContentPane(mainForm.mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
//        frame.setSize(600,300);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setTitle("简易提醒器0.2");

        try {
            frame.setIconImage(ImageIO.read(MainForm.class.getClass().getResource("/1.jpg")));
        } catch (IOException ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        }

        mainForm.initTimer();

        try {
            System.out.println(MainForm.class.getResource("/"));
            TrayIcon ti = new TrayIcon(ImageIO.read(MainForm.class.getResource("/1.jpg")));
            getSystemTray().add(ti);
            ti.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    frame.setVisible(true);
                }
            });
        } catch (Exception ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
